package secondriver.heathfood.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ScrollingView;
import android.view.Menu;
import android.widget.ScrollView;
import butterknife.Bind;
import butterknife.ButterKnife;
import secondriver.heathfood.HeathFoodApplication;
import secondriver.heathfood.R;
import secondriver.heathfood.adapter.ViewHolder;
import secondriver.heathfood.client.ApiClient;
import secondriver.heathfood.client.Constants;
import secondriver.heathfood.bean.FoodDetail;
import secondriver.heathfood.common.*;

/**
 * Author : secondriver
 */
public class FoodDetailActivity extends AbstractBaseActivity {

    final static int LoadingWhat = 0x01;
    final static int DialogWhat = 0x11;

    private ProgressDialog progressDialog;
    private int progress = 0;
    private int progressMax = 100;

    public ViewHolder.FoodDetailViewHolder mViewHolder;

    @Bind(R.id.food_detail_scrollview)
    public ScrollView scrollView;

    public ApiClient mApiClient;
    private Handler mHandler;

    private long foodId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        ButterKnife.bind(this);

        enableHomeButton();

        initVariable();
        initHandler();
    }

    private void initVariable() {
        mViewHolder = new ViewHolder.FoodDetailViewHolder(this);
        foodId = getIntent().getLongExtra(Constants.FOOD_ID, 10);
        mApiClient = ApiClient.build(this, Constants.FOOD_DETAIL_CACHE_NAME);

    }

    private void initHandler() {

        progressDialog = new ProgressDialog(this);
        progressDialog.setMax(progressMax);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.info_food_loading));

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case LoadingWhat:
                        if (progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        FoodDetail foodDetail = (FoodDetail) msg.obj;
                        if (foodDetail != FoodDetail.Null) {
                            setActionBarTitle(getString(R.string.heath_food_detail, foodDetail.getName()));
                            mViewHolder.setContent(foodDetail, mApiClient.getSimpleDiskCache());
                        }
                        break;
                    case DialogWhat:
                        if (progress > progressMax) {
                            progress = 0;
                            progressDialog.setProgress(progress);
                        } else {
                            progressDialog.incrementProgressBy(1);
                        }
                        sendEmptyMessageDelayed(DialogWhat, 100);
                        break;
                    default:
                        break;
                }
            }
        };

        final Message message = Message.obtain(mHandler);
        message.what = LoadingWhat;
        final Bundle bundle = new Bundle();
        bundle.putLong(Constants.FOOD_ID, foodId);
        BackgroundTaskExecutor.doTask(bundle, message, new BackgroundTasks.LoadFoodDetail(mApiClient));

        progressDialog.show();
        progressDialog.setProgress(progress > 0 ? progress : 0);
        mHandler.sendEmptyMessageDelayed(DialogWhat, 100);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.findItem(R.id.menu_item_screen_shot_save).setVisible(true);
        return true;
    }

    @Override
    public Bitmap screenShotScrollView() {
        return screenShot(scrollView);
    }
}